package com.xiaoa.gossip.cluster.listener;

import com.xiaoa.gossip.cluster.command.CurrentNodeState;
import com.xiaoa.gossip.cluster.command.PingNodeState;
import com.xiaoa.gossip.cluster.event.NoteConnectFailEvent;
import com.xiaoa.gossip.cluster.service.ClusterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import static com.xiaoa.gossip.cluster.command.NodeState.NODE_STATE_P_FAIL;

/**
 * @author luokaichuang
 * @time 2020/6/2
 */
@Component
public class NoteConnectFailListener implements ApplicationListener<NoteConnectFailEvent> {

    @Autowired
    private ClusterService clusterService;

    @Autowired
    private CurrentNodeState currentNodeState;

    @Override
    public void onApplicationEvent(NoteConnectFailEvent event) {

        // 将当前节点修改为pfail
        PingNodeState nodeState =  event.nodeState();
        nodeState.setState(NODE_STATE_P_FAIL);
        clusterService.addOrUpdateNode(nodeState ,currentNodeState );

    }
}
